package com.gxc.authcenter.system.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import com.gxc.authcore.system.domain.User;
import com.gxc.authcore.system.service.UserService;
import com.gxc.authcore.system.util.ParamUtil;
import com.gxc.dbcore.util.PageInfo;
import com.gxc.dbcore.util.PageList;
import com.gxc.webcore.util.JsonUtil;
import com.gxc.webcore.util.RequestUtil;

@Controller("system/userController")
public class UserController {

	@Autowired
	private UserService userService;

	/**
	 * 列出所有用户（页面）
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return new ModelAndView("system/user/user_list.vm");
	}

	/**
	 * 列出一页用户
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public ModelAndView grid(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map map = RequestUtil.getInMap(request);
		PageInfo pageInfo = PageInfo.getFromRequest(request);
		PageList pageList = userService.listPage(map, pageInfo);
		Map result = new HashMap();
		result.put("status", "success");
		result.put("data", pageList);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	/**
	 * 新建用户页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView create(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return new ModelAndView("system/user/user_create.vm");
	}

	/**
	 * 新建用户保存
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView createSave(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		User user = (User) RequestUtil.getInObject(request, User.class);
		// 检查找好是否已存在
		if (user.getId() == null || user.getId().trim().equals("")) {
			result.put("status", "failed");
		} else {
			User tempUser = userService.retrieve(user.getId());
			if (tempUser != null) {
				result.put("status", "failed");
			} else {
				user.setCreate_time(new Date());
				user.setUpdate_time(new Date());
				userService.create(user);
				result.put("status", "success");
				result.put("data", user);
			}
		}
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	/**
	 * 新建用户页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView edit(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		User user = userService.retrieve(id);
		Map model = new HashMap();
		model.put("user", user);
		return new ModelAndView("system/user/user_edit.vm", model);
	}

	/**
	 * 新建用户保存
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView editSave(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		User user = (User) RequestUtil.getInObject(request, User.class);
		user.setUpdate_time(new Date());
		int i = userService.update(user);
		result.put("status", "success");
		result.put("data", i);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	/**
	 * 删除一个用户
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		String id = request.getParameter("id");
		int i = userService.delete(id);
		result.put("status", "success");
		result.put("data", i);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	/**
	 * 删除多个用户
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView deleteBatch(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		String id = request.getParameter("id");
		String[] id_array = id.split(",");
		int i = userService.delete(id_array);
		result.put("status", "success");
		result.put("data", i);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	/**
	 * 查看用户
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView view(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		Map params = new HashMap();
		params.put("user_id", id);
		User user = userService.retrieve(id);
		List roleList = userService.listRelatedRole(params);
		Map model = new HashMap();
		model.put("user", user);
		model.put("roleList", roleList);
		return new ModelAndView("system/user/user_view.vm", model);
	}

	/**
	 * 检测账号是否已存在
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView checkExist(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map map = new HashMap();
		String id = request.getParameter("id");
		User user = userService.retrieve(id);
		if (user == null) {
			map.put("status", "success");
			map.put("data", "该账号有效，可以注册。");
		} else {
			map.put("status", "failed");
			map.put("data", "该账号已存在。");
		}
		String jsonResult = JsonUtil.object2json(map);
		response.getWriter().write(jsonResult);
		return null;
	}

	/**
	 * 获取用户关联角色的管理页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView listRole(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String user_id = request.getParameter("user_id");
		User user = userService.retrieve(user_id);
		Map model = new HashMap();
		model.put("user", user);
		return new ModelAndView("system/user/user_role_relate.vm", model);
	}

	/**
	 * 获取user关联的role
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public ModelAndView gridRole(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map map = RequestUtil.getInMap(request);
		PageInfo pageInfo = PageInfo.getFromRequest(request);
		PageList pageList = userService.listPageOfRole(map, pageInfo);
		Map result = new HashMap();
		result.put("status", "success");
		result.put("data", pageList);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	/**
	 * 关联user和role
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView createRoleRelate(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String user_id = request.getParameter("user_id");
		String role_id = request.getParameter("role_id");
		int i = userService.createRoleRelate(user_id, role_id);
		Map result = new HashMap();
		result.put("status", "success");
		result.put("data", i);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	/**
	 * 删除user和role的关联
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView deleteRoleRelate(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String user_id = request.getParameter("user_id");
		String role_id = request.getParameter("role_id");
		int i = userService.deleteRoleRelate(user_id, role_id);
		Map result = new HashMap();
		result.put("status", "success");
		result.put("data", i);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	/**
	 * 初始化密码
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView initPassword(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		String id = request.getParameter("id");
		User user = new User();
		user.setId(id);
		user.setPassword(ParamUtil.get("initPassword"));
		user.setUpdate_time(new Date());
		int i = userService.update(user);
		result.put("status", "success");
		result.put("data", i);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

}
